const catchUtil = {
	// 设置/添加cookie
	setCookie : (name,value,{maxAge,domain,path,secure}={}) => {
		let cookieText = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
		if(typeof maxAge == 'number'){
			cookieText += `;bmax-age=${maxAge}`;
		}
		if(domain){
			cookieText += `; domain=${domain}`;
		}
		if(path){
			cookieText += `; path=${path}`;
		}
		if(secure){
			cookieText += `; secure=${secure}`;
		}
		document.cookie = cookieText;
	},
	// 获取cookie
	getCookie : (name)=>{
		if(name){
			// 多个cookie通过;分割为多个由'键=值'组成的数组
			const cookieList = decodeURIComponent(document.cookie).split('; ');
			for(let val of cookieList){
				if(name == val.split('=')[0]){
					return val.split('=')[1]
				}
			}
		}
		return ''
	},
	
	// 删除cookie
	// const deleteCookie = (name)=>{
	// 	setCookie(name,null,-1)
	// }
	
	// 添加（缓存）localStorage
	setLocalStorage:(key,val)=>{
		if(key && val){
			localStorage.setItem(key,JSON.stringify(val));
		}
	},
	// 获取（缓存）localStorage
	getLocalStorage:(key)=>{
		const val = localStorage.getItem(key)
		if(val){
			return JSON.parse(val)
		}
	},
	
	// 添加（缓存）sessionStorage
	setSessionStorage:(key,val)=>{
		if(key && val){
			sessionStorage.setItem(key,JSON.stringify(val));
		}
	},
	// 获取（缓存）sessionStorage
	getSessionStorage:(key)=>{
		const val = sessionStorage.getItem(key)
		if(val){
			return JSON.parse(val)
		}else{
			return {}
		}
	}
}

export default catchUtil